package main

import (
	"fmt"
	"gomvc/app/router"
	"gomvc/app/utils"
	"image"
	"math"
	"os"
)

// "gomvc/app/models"

// "image/draw"
// "gomvc/app/router"
// "gorm.io/driver/mysql"
// "gorm.io/gorm"
// "gorm.io/gorm/schema"

//func includesNumber(number, target, tolerance float64) bool {
//	diff := math.Abs(number - target)
//	return diff <= tolerance
//}

func One(minLng, maxLat, step float64, precision float64) {
	//landM := make(map[[2]int]struct{})
	f1, _ := os.Open("./public/image/yh.png")

	num := 0

	defer f1.Close()
	img1, _, _ := image.Decode(f1)

	maxX, maxY := img1.Bounds().Max.X, img1.Bounds().Max.Y

	for i := 0; i <= maxX; i = i + (int(0.05 * 10000)) {
		for j := 0; j <= maxY; j = j + (int(0.05 * 10000)) {
			lng := float64(i)*0.0001 + minLng
			lat := maxLat - float64(j)*0.0001
			//创建一个txt文件，写入内容；根目录为 ./public/image/0.05/
			f, _ := os.Create("./public/image/0.05/" + utils.Float64ToFixedStr(lat, 2) + "_" + utils.Float64ToFixedStr(lng, 2) + ".txt")

			landNum := 0
			for i1 := 0; i1 < 50; i1++ {
				for j1 := 0; j1 < 50; j1++ {
					r, g, b, _ := img1.At(i+i1, j+j1).RGBA()
					if r == 0 && g == 0 && b == 0 {
						continue
					} else {
						landNum++
						lat1 := lat - float64(j1)*0.0001
						lng1 := lng + float64(i1)*0.0001
						//int(math.Round(lat * precision))
						f.WriteString(utils.Float64ToStr(math.Round(lat1*precision)) + "," + utils.Float64ToStr(math.Round(lng1*precision)) + "\n")
					}
				}
			}
			f.Close()
			if landNum == 0 {
				os.Remove("./public/image/0.05/" + utils.Float64ToFixedStr(lat, 2) + "_" + utils.Float64ToFixedStr(lng, 2) + ".txt")
			} else {
				num++
			}

		}
	}

	fmt.Println(num)
}

func Two(minLng, maxLat, step float64, precision float64) {
	//landM := make(map[[2]int]struct{})
	f1, _ := os.Open("./public/image/yh.png")

	num := 0

	defer f1.Close()
	img1, _, _ := image.Decode(f1)

	maxX, maxY := img1.Bounds().Max.X, img1.Bounds().Max.Y
	f, _ := os.Create("./public/image/0.001.txt")

	for i := 0; i <= maxX; i = i + (int(0.001 * 10000)) {
		for j := 0; j <= maxY; j = j + (int(0.001 * 10000)) {
			lng := float64(i)*0.0001 + minLng
			lat := maxLat - float64(j)*0.0001
			//创建一个txt文件，写入内容；根目录为 ./public/image/0.05/

			landNum := 0
			for i1 := 0; i1 < 10; i1++ {
				for j1 := 0; j1 < 10; j1++ {
					r, g, b, _ := img1.At(i+i1, j+j1).RGBA()
					if r == 0 && g == 0 && b == 0 {
						continue
					} else {
						landNum++
						//lat1 := lat - float64(j1)*0.0001
						//lng1 := lng + float64(i1)*0.0001
						////int(math.Round(lat * precision))
						//f.WriteString(utils.Float64ToStr(math.Round(lat1*precision)) + "," + utils.Float64ToStr(math.Round(lng1*precision)) + "\n")
						break
					}
				}
			}
			if landNum > 0 {
				f.WriteString(utils.Float64ToStr(math.Round(lat*precision)) + "," + utils.Float64ToStr(math.Round(lng*precision)) + "\n")
				num++
			}
			//if landNum == 0 {
			//	os.Remove("./public/image/0.05/" + utils.Float64ToFixedStr(lat, 2) + "_" + utils.Float64ToFixedStr(lng, 2) + ".txt")
			//} else {
			//	num++
			//}

		}
	}

	f.Close()

	fmt.Println(num)
}

func main() {
	//One(119, 38, 0.05, 10000)
	//Two(119, 38, 0.05, 1000)
	//fmt.Println(includesNumber(36.1244, 36.123, 0.001))

	//fmt.Println()
	//fileDir := ""

	//grayMap := make(map[int]struct{}, 200000)
	//
	//var wgTest sync.WaitGroup
	//wgTest.Add(1)
	//go func() {
	//	for i := 3000; i < 9000; i++ {
	//		grayMap[i] = struct{}{}
	//	}
	//	wgTest.Done()
	//}()
	//wgTest.Add(1)
	//go func() {
	//	for i := 1000; i < 2000; i++ {
	//		grayMap[i] = struct{}{}
	//	}
	//	wgTest.Done()
	//}()
	//wgTest.Wait()
	//
	//fmt.Println(len(grayMap))
	// wmapInt := map[[2]int]struct{}{
	// 	{1, 2}: {},
	// 	{2, 3}: {},
	// }
	// wmapStr := map[string]struct{}{}
	// for k, _ := range wmapInt {
	// 	key := strconv.Itoa(k[0]) + strconv.Itoa(k[1])
	// 	wmapStr[key] = struct{}{}
	// }
	// jsonData, _ := json.Marshal(wmapStr)
	// ioutil.WriteFile("test.json", jsonData, 0666)

	// jsonData, _ := ioutil.ReadFile("test.json")
	// wmapStr := map[string]struct{}{}
	// json.Unmarshal(jsonData, &wmapStr)
	// wmapInt := map[[2]int]struct{}{}
	// for k, _ := range wmapStr {
	// 	var arr [2]int
	// 	arr[0], _ = strconv.Atoi(k[0:1])
	// 	arr[1], _ = strconv.Atoi(k[1:2])
	// 	wmapInt[arr] = struct{}{}
	// }
	// _, is := wmapInt[[2]int{1, 2}]

	// a := 2.654
	// 向上取整
	// fmt.Println(int(a))
	// // 向下取整
	// fmt.Println(int(a) - 1)
	// 向上保留一位小数
	// fmt.Println(math.Floor(a*10)/10)
	// // 向下保留一位小数
	// fmt.Println(math.Ceil(a*10)/10)
	// // 四舍五入保留一位小数
	// fmt.Println(math.Round(a*10)/10)
	// err := models.InitDb()
	// if err != nil {
	// 	println(err)
	// }

	router.Init()

	// test()
}

// var wg sync.WaitGroup
// var mm sync.Mutex

// func test() {
// 	arr := []int{}
// 	for i := 0; i < 16; i++ {
// 		go func() {
// 			mm.Lock()
// 			arr = append(arr, i)
// 			mm.Unlock()
// 			wg.Done()
// 		}()
// 		wg.Add(1)
// 	}
// 	wg.Wait()
// 	println(len(arr))
// }

// func initGorm() (err error) {
// 	dsn := "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
// 	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
// 		NamingStrategy: schema.NamingStrategy{
// 			SingularTable: true,
// 		},
// 	})
// 	if err != nil {
// 		return err
// 	} else {
// 		return nil
// 	}
// }
